
package com.jt.tenant.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.InsOfferAdapter;
//import com.jt.tenant.conn.GetInsQuote;
//import com.jt.tenant.conn.SetInsCancelResult;
//import com.jt.tenant.dialog.InsCancelDialog;
import com.jt.tenant.conn.GetInsQuote;
import com.jt.tenant.dialog.TipDialog;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class InsQuoteActivity extends JTActivity implements InsOfferAdapter.Refrence,View.OnClickListener{

    private TextView title_name,commit,tv_next;

    private LinearLayout lin_call;

    private ListView mListView;

    private InsOfferAdapter mAdapter;

    private List<GetInsQuote.Quote> mList = new ArrayList<>();

    private List<GetInsQuote.Quote> showList = new ArrayList<>();

    private GetInsQuote getInsQuote;

    private String name,phone;

    private int check_position = -1;

    private String orderId;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_ins_quote);

        initView();

    }

    private void initView(){

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("保险报价");

        (commit = (TextView)findViewById(R.id.commit)).setText("取消查询");

        commit.setOnClickListener(this);

        getInsQuote = new GetInsQuote(new AsyCallBack<GetInsQuote.Info>(){

            @Override
            public void onSuccess(String toast, int type, GetInsQuote.Info info) throws Exception {

                super.onSuccess(toast, type, info);

                if(info.msg.equals("000")){

                    mList.clear();

                    mList.addAll(info.quoteList);

                    showList.clear();

                    showList.add(mList.get(0));

                    showList.get(0).flag = 1;

                    mAdapter.notifyDataSetChanged();

                    name = info.ownerName;

                    phone = info.phone;

                    orderId = info.orderId;

                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {

                super.onFail(toast, type);

                UtilToast.show(context,toast);

            }
        });

        getInsQuote.execute(this);

        (lin_call = (LinearLayout) findViewById(R.id.lin_ins_call)).setOnClickListener(this);

        (tv_next = (TextView)findViewById(R.id.tv_ins_quote_next)).setOnClickListener(this);

        mListView = (ListView)findViewById(R.id.lv_ins_quote_1);

        mListView.setAdapter(mAdapter = new InsOfferAdapter(context,showList,this));

//        refreshLinster = new RefreshLinster() {
//            @Override
//            public void refresh() {
//
//                setInsCancelResult.execute(InsQuoteActivity.this);
//
//            }
//        };

    }

    @Override
    public void refrence(int position,int type) {

        if(type == 0) {

            if (mList.get(position).flag == 0) {

                for (int i = 0; i < mList.size(); i++) {

                    mList.get(i).flag = 0;

                }

                mList.get(position).flag = 1;

            } else if (mList.get(position).flag == 1) {

                mList.get(position).flag = 0;

            }

        }else {

            if (mList.get(position).check == 0) {

                for (int i = 0; i < mList.size(); i++) {

                    mList.get(i).check = 0;

                }

                mList.get(position).check = 1;

                check_position = position;

            } else if (mList.get(position).check == 1) {

                mList.get(position).check = 0;

                check_position = -1;

            }




        }

        mAdapter.notifyDataSetChanged();

    }

    @Override
    public void onClick(View view) {

        super.onClick(view);

        switch (view.getId()){

            case R.id.lin_ins_call:

                Tools.Call(context,"4006115616");

                break;

            case R.id.tv_ins_quote_next:

//                if(check_position == -1 ){
//
//                    UtilToast.show(this,"请选择保险报价");
//
//                }else {

                    startActivity(new Intent(this, InsSupInfoActivity.class)

//                            .putExtra("ins_id", mList.get(check_position).id)

                            .putExtra("ins_id", orderId)

                            .putExtra("name", name)

                            .putExtra("phone", phone)

                    );

//                }

                break;

            case R.id.commit:

                TipDialog cancelDialog = new TipDialog(context);

                cancelDialog.setTip("确定取消报价查询？如果取消将丢失已填写数据");

                cancelDialog.setType(5);

                cancelDialog.show();

                break;



        }
    }

//    @Override
//    public void onRightClick(View v) {
//
//        super.onRightClick(v);
//
//        InsCancelDialog dialog = new InsCancelDialog(context);
//
//        dialog.setTip("确定取消报价查询？如果取消将丢失已填写数据");
//
//        dialog.show();
//
//    }

    public abstract class RefreshLinster {

        public abstract void refresh();

    }

}
